﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MiniFox.Components;
using MiniFox.Configurations;
using MiniFox.Web.Configurations;
using System.Reflection;

namespace MiniFox.Web.AppStarting
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class HttpStartBase : Component, IHttpStart
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="hostEnvironment"></param>
        /// <param name="webConfiguration"></param>
        protected HttpStartBase(IConfiguration configuration, IWebHostEnvironment hostEnvironment, WebConfiguration webConfiguration)
        {
            this.Configuration = configuration;
            this.Environment = hostEnvironment;
            this.WebConfiguration = webConfiguration;
        }
        /// <summary>
        /// 
        /// </summary>
        public IConfiguration Configuration { get; }
        /// <summary>
        /// 
        /// </summary>
        public IWebHostEnvironment Environment { get; }
        /// <summary>
        /// 
        /// </summary>
        public WebConfiguration WebConfiguration { get; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        public abstract void OnServicesConfiguring(IServiceCollection services);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        public abstract void OnApplicationConfiguring(IApplicationBuilder app);

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected virtual IList<Assembly> GetAssemblies()
        {

            var assemblyConfig = ConfigurationFactory.GetConfig<AssemblyConfiguration>();

            IList<Assembly> baseAssemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
            if (assemblyConfig == null || assemblyConfig.AssemblySettings == null || assemblyConfig.LoadAll)
            {
                var binDirectory = AppDomain.CurrentDomain.BaseDirectory;
                string[] allFiles = Directory.GetFiles(binDirectory, "*.dll", SearchOption.TopDirectoryOnly);
                foreach (string file in allFiles)
                {
                    var controllersAssembly = Assembly.LoadFrom(file);

                    if (baseAssemblies.Any(a => string.Equals(a.FullName, controllersAssembly.FullName)))
                    {
                        continue;
                    }
                    baseAssemblies.Add(controllersAssembly);
                }
            }
            else
            {
                foreach (AssemblySetting setting in assemblyConfig.AssemblySettings)
                {
                    var controllersAssembly = Assembly.LoadFrom(setting.FullName);
                    baseAssemblies.Add(controllersAssembly);
                }
            }
            List<Assembly> assemblies = new List<Assembly>(baseAssemblies);
            return assemblies;
        }

    }
}
